/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/<a href="https://www.kaaction.com">KAA</a>"><a href="https://www.kaaction.com">KAA</a></a> All rights reserved.
 */
package com.kaaction.modules.eb.service;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.kaaction.modules.eb.dao.SendMessageLogDao;
import com.kaaction.modules.eb.dao.UserLoginDao;
import com.kaaction.modules.eb.entity.UserLogin;
import com.kaaction.modules.httppost.Sendsms;
import com.kaaction.modules.sys.service.SystemService;

/**
 * 用户登入
 * @author huanghaiming
 *
 */
@Service
@Transactional(readOnly = true)
public class UserLoginService {
	
	@Autowired
	UserLoginDao userLoginDao;
	
	@Autowired
	SendMessageLogDao sendMessageLogDao;
	
	/**
	 * 用户登入
	 * @param uLogin
	 * @param request
	 * @return
	 */
	public Map<String, String> selectUserLogin(UserLogin uLogin,HttpServletRequest request) {
		
		Map<String,String> map = new HashMap<String,String>();
		
		UserLogin  ul = userLoginDao.getUserBy(uLogin);

		if(ul!=null){
			
			if (SystemService.validatePassword(uLogin.getPassword(), ul.getPassword())){
				request.getSession().setAttribute("ul_id", ul.getUlid());
				result(map,"登入成功",true);
			}else{
				result(map,"密码错误",false);
			}
			
		}else{
			result(map,"账号不存在",false);
		}
		
		return map;
	}
	
	/**
	 * 用户注册
	 * @param uLogin
	 * @param request
	 * @return
	 */
	@Transactional(readOnly = false)
	public Map<String, String> inserUserRegister(UserLogin uLogin, HttpServletRequest request) {
		
		Map<String,String> map = new HashMap<String,String>();
		
		map = check(uLogin,request);
		
		if(map!= null&&map.size()>0){
			return map;
		}
		
		UserLogin  ul = userLoginDao.getUserBy(uLogin);
		if(ul ==null){
			uLogin.setPassword(SystemService.entryptPassword(uLogin.getPassword()));
			userLoginDao.insert(uLogin);
			result(map,"注册成功",true);
		}else{
			result(map,"账号已存在",false);
		}
		
		return map;
	}

	
	private Map<String, String> check(UserLogin uLogin,HttpServletRequest request) {
		
		Map<String,String> map = new HashMap<String,String>();
		
		String mobileCode = uLogin.getMobile_code();//输入验证码
		String phone = uLogin.getPhone();//电话号码
		String password = uLogin.getPassword();//密码
		
		if("".equals(mobileCode)){
			result(map,"验证不能为空",false);
			return map;
		}
		String mobile_code = String.valueOf(request.getSession().getAttribute("mobile_code")) ;//获取验证信息
		if(!mobileCode.equals(mobile_code)){
			result(map,"验证不正确",false);
			return map;
		}
		if("".equals(phone)){
			result(map,"手机码不能为空",false);
			return map;
		}
		if("".equals(password)){
			result(map,"密码不能为空",false);
			return map;
		}
		
		return map;
	}
	
	
	@Transactional(readOnly = false)
	public void captchaSend(UserLogin uLogin, HttpServletRequest request) {
		
		Map<String,String> map = new HashMap<String,String>();
		
		int mobile_code = (int)((Math.random()*9+1)*100000);//验证信息

		String content ="感谢您选择喀嗒，" + mobile_code + "为您的验证码(15分钟有效)，请尽快完成验证，谢谢！";
		
		map.put("phone", uLogin.getPhone());
		map.put("content", content);
		map.put("type", "01");
		
		map = Sendsms.captcha(map);//map获取验证码是否成功log
		
		request.getSession().setAttribute("mobile_code",mobile_code);
		sendMessageLogDao.insetSendMegLog(map);
	}
	
	
	
	public Map<String,String> result(Map<String,String> map,String msg,boolean blean){
		if(blean)
			map.put("code", "success");
		else	
			map.put("code", "fail");
		map.put("msg", msg);
		return map;
	}

	@Transactional(readOnly = false)
	public void updatUser(UserLogin uLogin,HttpServletRequest request) {
		uLogin.setUlid(Integer.valueOf(request.getSession().getAttribute("ul_id").toString()));
		userLoginDao.update(uLogin);
	}

}
